package com.example.xm.adapter.HomeAdapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.xm.R;
import com.example.xm.base.BaseAdapter;
import com.example.xm.bean.HomeBean.HomeRecommendBean;
import com.example.xm.ui.home.activity.ImageActivity;
import com.example.xm.ui.home.activity.YouHomeActivity;
import com.example.xm.utiles.DateUtils;
import com.example.xm.customizing.ExpandableTextView;
import com.example.xm.utiles.SpUtils;
import com.example.xm.utiles.SystemUtils;

import java.util.ArrayList;
import java.util.List;

public class RecommendAdapter extends BaseAdapter {

    public RecommendAdapter(Context context, List data) {
        super(context, data);
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_recommend_item;
    }

    @SuppressLint("NewApi")
    @Override
    protected void bindData(BaseViewHolder viewHolder, Object o) {
        ImageView img = (ImageView) viewHolder.getViewById(R.id.iv_head);
        TextView usrename = (TextView) viewHolder.getViewById(R.id.txt_username);
        TextView date = (TextView) viewHolder.getViewById(R.id.txt_date);
        ImageView lv = (ImageView) viewHolder.getViewById(R.id.iv_level);
        TextView follow = (TextView) viewHolder.getViewById(R.id.txt_follow);
        ExpandableTextView des = (ExpandableTextView) viewHolder.getViewById(R.id.txt_des);
        RecyclerView rlv = (RecyclerView) viewHolder.getViewById(R.id.rlv_imgs);
        TextView follow_nums = (TextView) viewHolder.getViewById(R.id.tx_follow_nums);
        TextView comment = (TextView) viewHolder.getViewById(R.id.txt_comment);
        follow_nums.setVisibility(View.VISIBLE);
        comment.setVisibility(View.VISIBLE);
        HomeRecommendBean.DataBean.PostDetailBean detailBean = (HomeRecommendBean.DataBean.PostDetailBean) o;
        //设置圆角图片
        int round = SystemUtils.px2Dp(context, 60);
        RoundedCorners roundedCorners = new RoundedCorners(round);
        RequestOptions requestOptions = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(context).load(detailBean.getHeadUrl()).apply(requestOptions).into(img);
        int level = detailBean.getLevel();
        if (level == 1) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv1));
        } else if (level == 2) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv2));
        } else if (level == 3) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv3));
        } else if (level == 4) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv4));
        } else if (level == 5) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv5));
        } else if (level == 6) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv6));
        } else if (level == 7) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv7));
        } else if (level == 8) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv8));
        } else if (level == 9) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv9));
        } else {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv10));
        }

        if (!TextUtils.isEmpty(detailBean.getNickName())) {
            usrename.setText(detailBean.getNickName());
        }
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, YouHomeActivity.class);
                context.startActivity(intent);
            }
        });
        //显示时间
        if (!TextUtils.isEmpty(detailBean.getCreateTime())) {
            Long dateToTime = DateUtils.getDateToTime(detailBean.getCreateTime(), null);
            String standardDate = DateUtils.getStandardDate(dateToTime);
            date.setText(standardDate);
        }
        //内容的显示 ##包裹起来数据  @符号后面的数据
        String content = detailBean.getContent();
        //第一层切割
        //使用富文本修改 内容中的文字颜色
        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, 9, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.GREEN), 41, 48, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 70, 80, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        des.setMaxLines(70);
        des.setHasAnimation(false);
        des.setCloseInNewLine(true);
        des.setOpenSuffixColor(context.getResources().getColor(R.color.colorAccent));
        des.setCloseSuffixColor(context.getResources().getColor(R.color.colorAccent));
        des.setOriginalText(spannableString);


        //显示嵌套的GridView效果的图片

        List<HomeRecommendBean.DataBean.PostDetailBean.ImagesBean> images = detailBean.getImages();
        ImageAdapter imageAdapter = new ImageAdapter(context, images);
        GridLayoutManager manager = new GridLayoutManager(context, 3);
        rlv.setLayoutManager(manager);

//        rlv.addItemDecoration(new DividerItemDecoration(context, LinearLayout.VERTICAL));
//        rlv.addItemDecoration(new DividerItemDecoration(context,LinearLayout.HORIZONTAL));
        rlv.setAdapter(imageAdapter);
        follow_nums.setText(String.valueOf(detailBean.getLikeNumber()));
        comment.setText(String.valueOf(detailBean.getCommentNumber()));


        ImageAdapter imgsAda = new ImageAdapter(context, detailBean.getImages());
        List<HomeRecommendBean.DataBean.PostDetailBean.ImagesBean> imagesBeans = detailBean.getImages();
        ArrayList<String> imgs = new ArrayList<>();
        for (int i = 0; i < imagesBeans.size(); i++) {
            imgs.add(imagesBeans.get(i).getFilePath());
        }
        rlv.setAdapter(imgsAda);
        imgsAda.setOnClick(new onClick() {
            @Override
            public void click(int pos) {
                Intent intent = new Intent(context, ImageActivity.class);
                intent.putExtra("postion", pos);
                intent.putExtra("list", imgs);
                context.startActivity(intent);
            }
        });

    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        int res = 0;
        final float scale = context.getResources().getDisplayMetrics().density;
        if (dpValue < 0)
            res = -(int) (-dpValue * scale + 0.5f);
        else
            res = (int) (dpValue * scale + 0.5f);
        return res;
    }
}
